package com.none.web.controller.book;




import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.service.book.IBookAppService;

/**
 * bookApp管理Controller
 * 
 * @author Summer
 * 
 */
@Controller
@RequestMapping("bookapp")
public class BookAppController {

	@Resource(name = "bookAppService")
	IBookAppService bookAppService;
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * APP获取书的信息列表
	 * @param pager     分页信息
	 * @param keywords  搜索关键字
	 * @param category  书籍类型ID
	 * @param userName  当前App端登录的用户名
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("queryBookList")
	public Object queryBookList(@FormModel("pager") final Pager pager,String keywords,String category, String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_desp","book_open", "book_vol","startRow","catalog_id","borrowFlag"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("BookList", bookAppService.queryBookList(pager, keywords, category, userName));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	/**
	 * APP获取书的详细信息
	 * @param bookId    书籍ID
	 * @param userName  当前App端登录的用户名
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getDetailInfo")
	public Object getDetailInfo(Integer bookId, String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("book", bookAppService.getDetailInfo(bookId, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	/**
	 * APP获取预约取书日期及书的详情
	 * @param bookId    书籍ID
	 * @param userName  当前App端登录的用户名
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getPickUpDateAndBookinfo")
	public Object getPickUpDateAndBookinfo(Integer bookId, String currentTime,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("book", bookAppService.getDetailInfo(bookId, userName));
		data.put("pickUpDayInfo", bookAppService.getPickUpDate(currentTime, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 保存借书预约记录
	 * @param book_mgrNo
	 * @param phone
	 * @param pickupday
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("addBorrowBook")
	public Object addBorrowBook(String book_mgrNo, String phone, String pickupday, String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.addBorrowBook(book_mgrNo, phone, pickupday, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 查看借书预约记录
	 * @param book_mgrNo
	 * @param bookId
	 * @param borrowId
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("viewBorrowBook")
	public Object viewBorrowBook(String book_mgrNo,Integer bookId,String borrowId,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.viewBorrowBook(book_mgrNo, bookId, borrowId, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	/**
	 * 取消预约记录
	 * @param book_mgrNo
	 * @param bookId
	 * @param borrowId
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveCancelBorrowBook")
	public Object saveCancelBorrowBook(String book_mgrNo,Integer bookId,String borrowId,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		bookAppService.saveCancelBorrowBook(book_mgrNo, bookId, borrowId, userName);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 扫描二维码并解析
	 * @param content
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("readBorrowBook")
	public Object readBorrowBook(String content,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.readBorrowBook(content, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 取书
	 * @param book_mgrNo
	 * @param bookId
	 * @param borrowId
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveCheckOutBook")
	public Object saveCheckOutBook(String book_mgrNo,Integer bookId,String borrowId,String borrower,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.saveCheckOutBook(book_mgrNo, bookId, borrowId,borrower, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 还书
	 * @param book_mgrNo
	 * @param bookId
	 * @param borrowId
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveCheckInBook")
	public Object saveCheckInBook(String book_mgrNo,Integer bookId,String borrowId,String borrower,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.saveCheckInBook(book_mgrNo, bookId, borrowId,borrower, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 续借
	 * @param book_mgrNo
	 * @param bookId
	 * @param borrowId
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveRenewBook")
	public Object saveRenewBook(String book_mgrNo,Integer bookId,String borrowId,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.saveRenewBook(book_mgrNo, bookId, borrowId, userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 查询登录人当前预约或者借的书
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getMycurrentBook")
	public Object getMycurrentBook(String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.getMycurrentBook(userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 没有书可借时的推送消息
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveReminderNoBook")
	public Object saveReminderNoBook(String book_mgrNo,String userName) throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		bookAppService.saveReminderNoBook(book_mgrNo,userName);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 没有书可借时的推送消息
	 * @param userName
	 * @return
	 * @throws Exception
	 */
//	@ResponseBody
//	@RequestMapping("savePushReminder")
//	public Object savePushReminder() throws Exception {
//		
//		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
//		Map<String, Object> data = new HashMap<String, Object>();
//		bookAppService.savePushReminder();
//		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
//		return JSONUtil.toJSON(result, filter);
//	}
	
	/**
	 * 一个是到期还书前7天提醒用户还书或者renew，一个是过期还书后1天提醒用户书已经过期了
	 * 1、到期前7天和过期后1天，这个天数做成可配置的；
	 * 2、在中午12点推送提醒；
	 * 3、用户点击推送信息自动导航到borrow record页面。
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("pushBeforeDueOrAfterDue")
	public Object pushBeforeDueOrAfterDue() throws Exception {
		
		String[] filter = new String[] { "addtime", "book_catalog","book_open", "book_vol","borrowFlag","book_desp","book_isbn"};
		Map<String, Object> data = new HashMap<String, Object>();
		bookAppService.pushBeforeDueOrAfterDue();
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 取消预约之后，将对应的记录中的二维码字段置空，并删除对应的图片
	 * 该方法要定时执行
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("deleteQRImageAfterCancel")
	public Object deleteQRImageAfterCancel() throws Exception {
		
		String[] filter = new String[] {};
		Map<String, Object> data = new HashMap<String, Object>();
		bookAppService.deleteQRImageAfterCancel();
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 取书和还书之前判断状态
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("checkStatus")
	public Object checkStatus(String book_mgrNo,Integer bookId, String borrowId, String userName, String type) throws Exception {
		
		String[] filter = new String[] {};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Info", bookAppService.checkStatus(book_mgrNo, bookId, borrowId, userName, type));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
}
